# coding: utf-8
# 文件名称: 用计数器装饰阶乘.py
# 创建时间: 2021/5/23 17:29

# 需求1：求1*2*3*4*n
# 需求2：通过装饰器打印出每一次递归调用所需要的运行时长
import time

i = 0
def runtime(origin):
    def wrapper(sum):
        t1 = time.time()
        time.sleep(1)
        res = origin(sum)
        t2 = time.time()
        run_time = t2 - t1
        global i
        i += 1
        print(f'第{i}次递归调用时长：{run_time}')
        print(res)
        return res
    return wrapper

# 利用递归实现
@runtime  #相当于：sum_result = rumtime(sum_result)，即把sum_result函数本身当做参数传递给runtime，并将返回值赋值给sum_result
def sum_result(n):
    if n == 1:
        return 1
    return n * sum_result(n-1)


#sum_result = runtime(sum_result)
result = sum_result(4)
#print(result)